package cn.com.surker.sale.service.impl;

import cn.com.surker.base.pump.dao.PumpNormDao;
import cn.com.surker.base.pump.dao.PumpNormOrgDao;
import cn.com.surker.base.pump.dao.PumpTypeDao;
import cn.com.surker.base.pump.dao.PumpTypeOrgDao;
import cn.com.surker.base.pump.entity.PumpNorm;
import cn.com.surker.base.pump.entity.PumpNormOrg;
import cn.com.surker.base.pump.entity.PumpType;
import cn.com.surker.base.pump.entity.PumpTypeOrg;
import cn.com.surker.base.strength.entity.BaseStrengthOrg;
import cn.com.surker.common.Code;
import cn.com.surker.database.JDBCTemplateCache;
import cn.com.surker.enums.system.PriceTaxEnum;
import cn.com.surker.sale.dao.SaleContractDao;
import cn.com.surker.sale.dao.SaleContractPumpPriceDao;
import cn.com.surker.sale.entity.SaleContract;
import cn.com.surker.sale.entity.SaleContractPumpPrice;
import cn.com.surker.sale.entity.SaleContractStrengthPrice;
import cn.com.surker.sale.service.SaleContractPumpPriceBaseService;
import cn.com.surker.system.dao.*;
import cn.com.surker.system.entity.DataSourceInfo;
import cn.com.surker.system.entity.SysMessageRecord;
import cn.com.surker.system.entity.SysOrigin;
import cn.com.surker.system.entity.SysUser;
import cn.com.surker.util.DateUtil;
import cn.com.surker.util.StringUtils;
import cn.com.surker.vo.sale.SaleContractVo;
import cn.com.surker.wechat.WechatOaGroupApi;
import cn.com.surker.wechat.WechatSurkerGroupApi;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.PreparedStatementCreator;
import org.springframework.jdbc.support.GeneratedKeyHolder;
import org.springframework.jdbc.support.KeyHolder;

import javax.annotation.Resource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.*;

/**
 * @Author lucky
 * @create 2024年07月10 16:00
 * @desc
 */
public abstract class SaleContractPumpPriceBaseServiceImpl extends ServiceImpl<SaleContractPumpPriceDao, SaleContractPumpPrice> implements SaleContractPumpPriceBaseService {

    @Resource
	protected SaleContractPumpPriceDao saleContractPumpPriceDao;
    @Resource
    protected DataSourceInfoDao dataSourceInfoDao;
    @Resource
    protected SaleContractDao saleContractDao;
    @Resource
    private PumpNormOrgDao pumpNormOrgDao;
    @Resource
    private PumpTypeOrgDao pumpTypeOrgDao;
    @Resource
    private PumpNormDao pumpNormDao;
    @Resource
    private PumpTypeDao pumpTypeDao;
    @Resource
    private SysMessageRecordDao messageRecordDao;
    @Resource
    private SysUserPositionDao userPositionDao;
    @Resource
    private SysUserDao userDao;
    @Resource
    private SysOriginDao originDao;


    /**
     * 查询销售合同泵送单价信息
     * @param contractId
     * @return
     */
    @Override
    public Map<String, List<SaleContractPumpPrice>> findByContract(Long contractId) {
        if (StringUtils.isNull(contractId))
            return null;
        QueryWrapper<SaleContractPumpPrice> query = new QueryWrapper<>();
        query.eq("status", 0);
        query.eq("contract_id", contractId);
        query.orderByDesc("take_time");
        List<SaleContractPumpPrice> priceList = saleContractPumpPriceDao.selectList(query);
        if (priceList == null || priceList.size() == 0)
            return null;
        Map<String, List<SaleContractPumpPrice>> map = new LinkedHashMap<>();
        for (int i = 0; i < priceList.size(); i++) {
            SaleContractPumpPrice price = priceList.get(i);
            String takeTime = DateUtil.convertString(price.getTakeTime());
            if (map.containsKey(takeTime)) {
                List<SaleContractPumpPrice> tempList = map.get(takeTime);
                tempList.add(price);
                map.put(takeTime, tempList);
            } else {
                List<SaleContractPumpPrice> tempList = new ArrayList<>();
                tempList.add(price);
                map.put(takeTime, tempList);
            }
        }
        return map;
    }

    /**
     * 新增砼单价信息
     * @param orgId
     * @param contractId 合同ID
     * @param pumpTypeId 泵送种类
     * @param pumpNormId 泵送设备
     * @param price 单价
     * @param takeTime 生效时间
     * @param remark
     * @param creator
     * @return
     */
    @Override
    public Integer savePump(Long orgId, Long contractId, Long pumpTypeId, Long pumpNormId, Double price, Double taxPrice, String takeTime, String remark, String creator) {
        if (StringUtils.isAllParamNull(orgId, contractId, pumpTypeId, pumpNormId, price, takeTime))
            return Code.PARAM_EMPTY.getCode();
        if (price < 1)
            return Code.PRICE_LT1.getCode();
        Date nowDate = new Date();
        Date takeDate = DateUtil.convertDate(takeTime);

        SaleContractVo saleContract = saleContractDao.selectById(contractId);
        if (saleContract == null)
            return Code.CONTRACT_EMPTY.getCode();
        SaleContractPumpPrice spp = saleContractPumpPriceDao.findTakeTime(orgId, contractId, pumpTypeId, pumpNormId, takeDate);
        if (spp != null) {
            spp.setPrice(price);
            spp.setTaxPrice(taxPrice);
            spp.setRemark(remark);
            spp.setUpdateTime(nowDate);
            spp.setUpdator(creator);
            Integer code = saleContractPumpPriceDao.updateById(spp);
            if (code > 0) {
                try {
                    sendMessage(saleContract, pumpTypeId, pumpNormId, spp.getPrice(), price, taxPrice,
                            takeTime, remark, creator ,1);
                } catch (Exception e) {
                    log.error("修改泵送价格异常" + e.getMessage());
                }
                return 0;
            }
            return -1;
        }

        DataSourceInfo ds = dataSourceInfoDao.queryDSByOriginId(orgId);
        if (ds == null || ds.getStatus() == 1)
            return Code.STATION_ERROR.getCode();
        if (ds.getType() == 1) {
            JdbcTemplate jdbcTemplate = JDBCTemplateCache.getLocalJdbcTemplate(ds);
            PumpNormOrg pumpNormOrg = pumpNormOrgDao.findByNormOrOrg(pumpNormId, orgId);
            if (pumpNormOrg == null) {
                PumpNorm pumpNorm = pumpNormDao.selectById(pumpNormId);
                if (pumpNorm == null)
                    return Code.ERP_PUMP_NORMS_EMPTY.getCode();
                String normSql = "select * from Sys_Field_Definition_T where tableName = 'Equipment_FileManagement_T' " +
                        "and fieldName = 'PumpNorms' and Definition = '" + pumpNorm.getName() + "'";
                List<Map<String, Object>> mapList = jdbcTemplate.queryForList(normSql);
                if (mapList== null || mapList.size() == 0) {
                    normSql = "select top 1 * from Sys_Field_Definition_T where tableName = 'Equipment_FileManagement_T' and fieldName = 'PumpNorms' \n" +
                            "order by status desc";
                    mapList = jdbcTemplate.queryForList(normSql);
                    Long erpId = StringUtils.convertLong(mapList.get(0).get("Status")) + 1;
                    pumpNormOrg = new PumpNormOrg(orgId, erpId, pumpNormId, 0, "", creator, nowDate);
                    pumpNormOrgDao.insert(pumpNormOrg);
                    String insertSql = "insert into Sys_Field_Definition_T (tablename,FieldName,ChineseName," +
                            "Definition,Status,CreatorID,CreationTime,ReviseID,ReviseTime,Parameter1) \n" +
                            "values('Equipment_FileManagement_T','PumpType','泵送设备种类',?,?,1,?,1,?,0)";
                    Object[] obj = {pumpNorm.getName(), erpId, DateUtil.convertString(nowDate), DateUtil.convertString(nowDate)};
                    jdbcTemplate.update(insertSql, obj);
                } else {
                    Long erpId = StringUtils.convertLong(mapList.get(0).get("Status"));
                    pumpNormOrg = new PumpNormOrg(orgId, erpId, pumpNormId, 0, "", creator, nowDate);
                    pumpNormOrgDao.insert(pumpNormOrg);
                }
            }

            PumpTypeOrg pumpTypeOrg = pumpTypeOrgDao.findByTypeOrOrg(pumpTypeId, orgId);
            if (pumpTypeOrg == null) {
                PumpType pumpType = pumpTypeDao.selectById(pumpNormId);
                if (pumpType == null)
                    return Code.ERP_PUMP_TYPE_EMPTY.getCode();
                String normSql = "select * from Sys_Field_Definition_T where tableName = 'Equipment_FileManagement_T' " +
                        "and fieldName = 'PumpType' and Definition = '" + pumpType.getName() + "'";
                List<Map<String, Object>> mapList = jdbcTemplate.queryForList(normSql);
                if (mapList== null || mapList.size() == 0) {
                    normSql = "select top 1 * from Sys_Field_Definition_T where tableName = 'Equipment_FileManagement_T' and fieldName = 'PumpType' \n" +
                            "order by status desc";
                    mapList = jdbcTemplate.queryForList(normSql);
                    Long erpId = StringUtils.convertLong(mapList.get(0).get("Status")) + 1;
                    pumpTypeOrg = new PumpTypeOrg(orgId, erpId, pumpTypeId, 0, "", creator, nowDate);
                    pumpTypeOrgDao.insert(pumpTypeOrg);
                    String insertSql = "insert into Sys_Field_Definition_T (tablename,FieldName,ChineseName," +
                            "Definition,Status,CreatorID,CreationTime,ReviseID,ReviseTime,Parameter1) \n" +
                            "values('Equipment_FileManagement_T','PumpType','泵送设备种类',?,?,1,?,1,?,0)";
                    Object[] obj = {pumpType.getName(), erpId, DateUtil.convertString(nowDate), DateUtil.convertString(nowDate)};
                    jdbcTemplate.update(insertSql, obj);
                } else {
                    Long erpId = StringUtils.convertLong(mapList.get(0).get("Status"));
                    pumpTypeOrg = new PumpTypeOrg(orgId, erpId, pumpTypeId, 0, "", creator, nowDate);
                    pumpTypeOrgDao.insert(pumpTypeOrg);
                }
            }
            Long erpId = null;
            if (!StringUtils.isNull(saleContract.getErpid()) && saleContract.getErpid() > 0) {
                erpId = saleContract.getErpid();
            } else {
                String contractSql = "select top 1 * from market_contract_t " +
                        " where isvalid = 1 and U_Code = '" + saleContract.getCode() + "' order by id desc";
                List<Map<String, Object>> list = jdbcTemplate.queryForList(contractSql);
                if (list == null || list.size() == 0)
                    return Code.ERP_CONTRACT_EMPTY.getCode();
                erpId = StringUtils.convertLong(list.get(0).get("ID"));
            }


            KeyHolder keyHolder = new GeneratedKeyHolder();
            String addSql = "insert into Market_Contract_PumpPrice_T (Market_Contract_ID,Model,StartDatetime,Price,Remarks,CreatorID,CreationTime,IsValid,PumpType,PumpNorms) \n" +
                    "  values (?,?,?,?,?,?,?,?,?,?)";
            Object[] params = {erpId,0, takeTime,price,remark,1,DateUtil.convertString(nowDate),1, pumpTypeOrg.getErpId(), pumpNormOrg.getErpId()};
            jdbcTemplate.update(new PreparedStatementCreator() {
                @Override
                public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
                    PreparedStatement ps = connection.prepareStatement(addSql, new String[]{"ID"});
                    for (int i = 0; i < params.length; i++) {
                        ps.setObject(i + 1, params[i]);
                    }
                    return ps;
                }
            }, keyHolder);
            long taskId = keyHolder.getKey().longValue();
        }

        SaleContractPumpPrice pumpPrice = new SaleContractPumpPrice(orgId, contractId, pumpTypeId,pumpNormId,
                price, takeDate, 0, remark, creator, nowDate);
        pumpPrice.setTaxPrice(taxPrice);
        Integer code = saleContractPumpPriceDao.insert(pumpPrice);
        if (code > 0) {
            try {
                sendMessage(saleContract, pumpTypeId, pumpNormId, price, taxPrice,0.0,
                        takeTime, remark, creator, 0);
            } catch (Exception e) {
                log.error("修改泵送价格异常" + e.getMessage());
            }
            return 0;
        }

        return -1;
    }
    /**
     *
     * @param id
     * @param pumpTypeId 泵送种类
     * @param pumpNormId 泵送设备
     * @param price 单价
     * @param takeTime 生效时间
     * @param remark
     * @param creator
     * @return
     */
    @Override
    public Integer updatePump(Long id, Long pumpTypeId, Long pumpNormId, Double price, Double taxPrice, String takeTime, String remark, String creator) {
        if (StringUtils.isNull(id))
            return Code.PARAM_EMPTY.getCode();
        SaleContractPumpPrice pumpPrice = saleContractPumpPriceDao.selectById(id);
        Date nowDate = new Date();
        if (pumpPrice == null || pumpPrice.getStatus() == 1)
            return Code.ERROR_DATA.getCode();
        Double oldPrice = pumpPrice.getPrice();
        if (!StringUtils.isNull(price) && price > 0)
            pumpPrice.setPrice(price);
        if (!StringUtils.isNull(takeTime)) {
            Date takeDate = DateUtil.convertDate(takeTime);
            pumpPrice.setTakeTime(takeDate);
        }
        if (!StringUtils.isNull(pumpTypeId))
            pumpPrice.setPumpTypeId(pumpTypeId);
        if (!StringUtils.isNull(pumpNormId))
            pumpPrice.setPumpNormId(pumpNormId);
        if (!StringUtils.isNull(remark))
            pumpPrice.setRemark(remark);
        if (!StringUtils.isNull(taxPrice) && taxPrice > 0)
            pumpPrice.setTaxPrice(taxPrice);
        pumpPrice.setUpdateTime(nowDate);
        pumpPrice.setUpdator(creator);
        Integer code = saleContractPumpPriceDao.updateById(pumpPrice);
        if (code > 0) {
            if (!StringUtils.isNull(price) && price > 0) {
                try {
                    SaleContractVo contract = saleContractDao.selectById(pumpPrice.getContractId());
                    sendMessage(contract, pumpTypeId, pumpNormId, oldPrice, price,taxPrice,
                            DateUtil.convertString(pumpPrice.getTakeTime()), pumpPrice.getRemark(), creator,1);
                } catch (Exception e) {
                    log.error("修改泵送价格异常" + e.getMessage());
                }
            }
            return 0;
        }
        return -1;
    }
    /**
     * 禁用合同
     * @param id
     * @param status
     * @return
     */
    @Override
    public Integer updateStatus(Long id, Integer status, String creator) {
        if (StringUtils.isNull(id))
            return Code.PARAM_EMPTY.getCode();
        SaleContractPumpPrice strengthPrice = saleContractPumpPriceDao.selectById(id);
        Date nowDate = new Date();
        if (strengthPrice == null)
            return Code.ERROR_DATA.getCode();
        strengthPrice.setStatus(status);
        strengthPrice.setUpdateTime(nowDate);
        strengthPrice.setUpdator(creator);
        Integer code = saleContractPumpPriceDao.updateById(strengthPrice);
        if (code > 0)
            return 0;
        return -1;
    }
    /**
     *
     * @param pumpList
     * @param contractId
     * @param creator
     * @return
     */
    @Override
    public Integer saveBatchPump(List<SaleContractPumpPrice> pumpList, Long orgId, Long contractId, String creator) {
        if (pumpList == null || pumpList.size() == 0)
            return 0;
        List<SaleContractPumpPrice> tempList = new ArrayList<>();
        DataSourceInfo ds = dataSourceInfoDao.queryDSByOriginId(orgId);
        if (ds == null || ds.getStatus() == 1)
            return Code.STATION_ERROR.getCode();
        JdbcTemplate jdbcTemplate = JDBCTemplateCache.getLocalJdbcTemplate(ds);
        for (int i = 0; i < pumpList.size(); i++) {
            Date nowDate = new Date();
            SaleContractPumpPrice pumpPrice = pumpList.get(i);
            if (StringUtils.isNull(pumpPrice.getTakeTime()))
                continue;
            if (StringUtils.isNull(pumpPrice.getPumpNormId()))
                continue;
            if (StringUtils.isNull(pumpPrice.getPumpTypeId()))
                continue;
            if (StringUtils.isNull(pumpPrice.getPrice()))
                continue;
            Date takeDate = pumpPrice.getTakeTime();
            Long pumpNormId = pumpPrice.getPumpNormId();
            Long pumpTypeId = pumpPrice.getPumpTypeId();
            if (!StringUtils.isNull(pumpPrice.getId())) {
                pumpPrice.setUpdator(creator);
                pumpPrice.setUpdateTime(nowDate);
                tempList.add(pumpPrice);
                continue;
            }
            Double price = pumpPrice.getPrice();
            Double taxPrice = pumpPrice.getTaxPrice();
            SaleContract saleContract = saleContractDao.selectById(contractId);
            if (saleContract == null)
                return Code.CONTRACT_EMPTY.getCode();
            if (StringUtils.isNull(price)) {//计算含税价
                if (!StringUtils.isNull(taxPrice))
                    price = StringUtils.calcTaxPrice(taxPrice, saleContract.getRate(), PriceTaxEnum.NO.getCode());
            } else if (StringUtils.isNull(taxPrice)){
                if (!StringUtils.isNull(price))
                    taxPrice = StringUtils.calcTaxPrice(price, saleContract.getRate(), PriceTaxEnum.YES.getCode());
            }

            SaleContractPumpPrice scp = saleContractPumpPriceDao.findTakeTime(orgId, contractId, pumpTypeId, pumpNormId, takeDate);
            if (scp != null) {
                scp.setPrice(price);
                scp.setTaxPrice(taxPrice);
                scp.setUpdator(creator);
                scp.setUpdateTime(nowDate);
                tempList.add(scp);
                continue;
            }

            PumpNormOrg pumpNormOrg = pumpNormOrgDao.findByNormOrOrg(pumpNormId, orgId);
            if (pumpNormOrg == null) {
                PumpNorm pumpNorm = pumpNormDao.selectById(pumpNormId);
                if (pumpNorm == null)
                    return Code.ERP_PUMP_NORMS_EMPTY.getCode();
                Long erpId = -1l;
                if (ds.getType() == 1) {
                    String normSql = "select * from Sys_Field_Definition_T where tableName = 'Equipment_FileManagement_T' " +
                            "and fieldName = 'PumpNorms' and Definition = '" + pumpNorm.getName() + "'";
                    List<Map<String, Object>> mapList = jdbcTemplate.queryForList(normSql);
                    if (mapList== null || mapList.size() == 0) {
                        normSql = "select top 1 * from Sys_Field_Definition_T where tableName = 'Equipment_FileManagement_T' and fieldName = 'PumpNorms' \n" +
                                "order by status desc";
                        mapList = jdbcTemplate.queryForList(normSql);
                        erpId = StringUtils.convertLong(mapList.get(0).get("Status")) + 1;
//                        pumpNormOrg = new PumpNormOrg(orgId, erpId, pumpNormId, 0, "", creator, nowDate);
//                        pumpNormOrgDao.insert(pumpNormOrg);
                        String insertSql = "insert into Sys_Field_Definition_T (tablename,FieldName,ChineseName," +
                                "Definition,Status,CreatorID,CreationTime,ReviseID,ReviseTime,Parameter1) \n" +
                                "values('Equipment_FileManagement_T','PumpNorms','泵送设备种类',?,?,1,?,1,?,0)";
                        Object[] obj = {pumpNorm.getName(), erpId, DateUtil.convertString(nowDate), DateUtil.convertString(nowDate)};
                        jdbcTemplate.update(insertSql, obj);
                    } else {
                        erpId = StringUtils.convertLong(mapList.get(0).get("Status"));

                    }
                }
                pumpNormOrg = new PumpNormOrg(orgId, erpId, pumpNormId, 0, "", creator, nowDate);
                pumpNormOrgDao.insert(pumpNormOrg);
            }

            PumpTypeOrg pumpTypeOrg = pumpTypeOrgDao.findByTypeOrOrg(pumpTypeId, orgId);
            if (pumpTypeOrg == null) {
                PumpType pumpType = pumpTypeDao.selectById(pumpNormId);
                if (pumpType == null)
                    return Code.ERP_PUMP_TYPE_EMPTY.getCode();
                Long erpId = -1l;
                if (ds.getType() == 1) {
                    String normSql = "select * from Sys_Field_Definition_T where tableName = 'Equipment_FileManagement_T' " +
                            "and fieldName = 'PumpType' and Definition = '" + pumpType.getName() + "'";
                    List<Map<String, Object>> mapList = jdbcTemplate.queryForList(normSql);
                    if (mapList== null || mapList.size() == 0) {
                        normSql = "select top 1 * from Sys_Field_Definition_T where tableName = 'Equipment_FileManagement_T' and fieldName = 'PumpType' \n" +
                                "order by status desc";
                        mapList = jdbcTemplate.queryForList(normSql);
                        erpId = StringUtils.convertLong(mapList.get(0).get("Status")) + 1;
                        String insertSql = "insert into Sys_Field_Definition_T (tablename,FieldName,ChineseName," +
                                "Definition,Status,CreatorID,CreationTime,ReviseID,ReviseTime,Parameter1) \n" +
                                "values('Equipment_FileManagement_T','PumpType','泵送设备种类',?,?,1,?,1,?,0)";
                        Object[] obj = {pumpType.getName(), erpId, DateUtil.convertString(nowDate), DateUtil.convertString(nowDate)};
                        jdbcTemplate.update(insertSql, obj);
                    } else {
                        erpId = StringUtils.convertLong(mapList.get(0).get("Status"));
                    }
                }
                pumpTypeOrg = new PumpTypeOrg(orgId, erpId, pumpTypeId, 0, "", creator, nowDate);
                pumpTypeOrgDao.insert(pumpTypeOrg);
            }
            Long erpId = null;
            if (!StringUtils.isNull(saleContract.getErpid()) && saleContract.getErpid() > 0) {
                erpId = saleContract.getErpid();
            } else {
                if (ds.getType() == 1) {
                    String contractSql = "select top 1 * from market_contract_t " +
                            " where isvalid = 1 and U_Code = '" + saleContract.getCode() + "' order by id desc";
                    List<Map<String, Object>> list = jdbcTemplate.queryForList(contractSql);
                    if (list == null || list.size() == 0)
                        return Code.ERP_CONTRACT_EMPTY.getCode();
                    erpId = StringUtils.convertLong(list.get(0).get("ID"));
                }
            }

            if (ds.getType() == 1) {
                KeyHolder keyHolder = new GeneratedKeyHolder();
                String addSql = "insert into Market_Contract_PumpPrice_T (Market_Contract_ID,Model,StartDatetime,Price,Remarks,CreatorID,CreationTime,IsValid,PumpType,PumpNorms) \n" +
                        "  values (?,?,?,?,?,?,?,?,?,?)";
                Object[] params = {erpId,0, DateUtil.convertString(takeDate),price,pumpPrice.getRemark(),1,DateUtil.convertString(nowDate),1, pumpTypeOrg.getErpId(), pumpNormOrg.getErpId()};
                jdbcTemplate.update(new PreparedStatementCreator() {
                    @Override
                    public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
                        PreparedStatement ps = connection.prepareStatement(addSql, new String[]{"ID"});
                        for (int i = 0; i < params.length; i++) {
                            ps.setObject(i + 1, params[i]);
                        }
                        return ps;
                    }
                }, keyHolder);
                long taskId = keyHolder.getKey().longValue();
            }
            SaleContractPumpPrice pumpPrice1 = new SaleContractPumpPrice(orgId, contractId, pumpTypeId,pumpNormId,
                    price, takeDate, 0, pumpPrice.getRemark(), creator, nowDate);
            pumpPrice1.setTaxPrice(taxPrice);
            tempList.add(pumpPrice1);
        }
        if (tempList == null)
            return -1;
        Integer code = saleContractPumpPriceDao.saveBatch(tempList);
        if (code > 0)
            return 0;
        return -1;
    }


    private void sendMessage(SaleContractVo contract, Long pumpTypeId, Long pumpNormId, Double price,Double taxPrice, Double newPrice,
                             String takeTime, String remark, String creator, Integer updateType) {
        String touser = "lucky|1lt1556|";
        String toname = "万强|罗涛|";
        String message = "";
        Date nowDate = new Date();
        String title = "";//推送标题
        String orgName = "";
        if (!StringUtils.isNull(contract.getOrgId())) {
            SysOrigin origin = originDao.selectById(contract.getOrgId());
            if (origin != null)
                orgName = origin.getOriginName();
        }
        if (updateType == 0) {
            message = "【工程泵送单价新增通知】 " + orgName + " 编号:" + contract.getCode();
            message += "\n工程名称:" + contract.getClientName() + "[" + contract.getName() + "]";
            message += "\n生效时间:" + takeTime;
            PumpNorm pumpNorm = pumpNormDao.selectById(pumpNormId);
            if (pumpNorm == null)
                return;
            PumpType pumpType= pumpTypeDao.selectById(pumpTypeId);
            if (pumpNorm == null)
                return;
            message += "\n规格类型:" + pumpType.getName() + "[" + pumpNorm.getName() + "]" + ",单价:" + price + "元。";
            message += "\n含税单价:" + taxPrice + "元。";
            if (!StringUtils.isNull(remark))
                message += "备注:" + remark;
        } else if (updateType == 1){
            message = "【工程泵送单价变更通知】 " + orgName + " 编号:" + contract.getCode();
            message += "\n工程名称:" + contract.getClientName() + "[" + contract.getName() + "]";
            message += "\n生效时间:" + takeTime;
            PumpNorm pumpNorm = pumpNormDao.selectById(pumpNormId);
            if (pumpNorm == null)
                return;
            PumpType pumpType= pumpTypeDao.selectById(pumpTypeId);
            if (pumpNorm == null)
                return;
            message += "\n规格类型:" + pumpType.getName() + "[" + pumpNorm.getName() + "]" + ",单价:" + price + "元 -> " + newPrice + "元。";
            message += "\n含税单价:" + taxPrice + "元。";
            if (!StringUtils.isNull(remark))
                message += "备注:" + remark;
        }
        if (!StringUtils.isNull(creator)) {
            message += "\n操作人:" + creator;
        }
        message += "\n操作时间:" + DateUtil.convertString(nowDate);
        List<Map<String, Object>> list = userPositionDao.hasGeneralManager(contract.getOrgId(), "销售内业", "信控专员");
        if (!StringUtils.isNull(contract.getSales())) {
            SysUser user = userDao.queryByName(contract.getSales());
            if (user != null && toname.indexOf(user.getName()) == -1) {
                toname += user.getName() + "|";
                touser += user.getUsername() + "|";
            }
        }
        if (list != null && list.size() > 0) {
            for (int i = 0; i < list.size(); i++) {
                Map<String, Object> temp = list.get(i);
                String name = StringUtils.convertString(temp.get("name"));
                if (!StringUtils.isNull(name) && toname.indexOf(name) == -1) {
                    toname += temp.get("name") + "|";
                    touser += temp.get("username") + "|";
                }
            }
        }

        toname = toname.substring(0, toname.length() - 1);
        touser = touser.substring(0, touser.length() - 1);
        SysMessageRecord record = new SysMessageRecord(touser, toname, "", WechatOaGroupApi.corpID, "", contract.getOrgId(),
                "", 1, 1, title, "",
                message, "", 1, 0, "", "", nowDate);
        messageRecordDao.insert(record);
        WechatOaGroupApi.sendTextMessage(WechatOaGroupApi.corpID, WechatOaGroupApi.corpSecret,
                WechatOaGroupApi.agentId, touser, message);
    }

}
